*This file was updated in October 2008 by Kenley Pelzer to create some loops that make the program more concise;
# delimit ;  
set more 1 ;  
local UNIX 0;
drop _all;
clear all;
capture log close;
set maxvar 32000;
set mem 1000m;

*global folder "d:\Dropbox\";
global folder "C:\Users\ebf26\Dropbox\";
*global folder "C:\Users\Ali\Documents";
*global folder "C:\Users\uctpkp0\Desktop";

*global folder "C:\Users\Jeremy\Dropbox";
*global folder "\\econ-san-fs\Home4\uctpkp0\SystemFolder\Windows7\Desktop";


         local saved  "$folder\hrs\merge\";
         local trackd "$folder\hrs\tracker\";
         local wave1d "$folder\hrs\wave1\";
         local wave2d "$folder\hrs\wave2\";
         local wave3d "$folder\hrs\wave3\";
         local wave4d "$folder\hrs\wave4\";
         local wave5d "$folder\hrs\wave5\";
		 local wave6d "$folder\hrs\wave6\";
         local wave7d "$folder\hrs\wave7\";
         local wave8d "$folder\hrs\wave8\";
		 local wave9d "$folder\hrs\wave9\";
		 local wave10d "$folder\hrs\wave10\";
		 local wave11d "$folder\hrs\wave11\";
		 local wave12d "$folder\hrs\wave12\";
		 local wave13d "$folder\hrs\wave13\";
		 local wave14d "$folder\hrs\wave14\";
		 local wave15d "$folder\hrs\wave15\";
         local aheadw1 "$folder\hrs\aheadw1\";
         local aheadw2 "$folder\hrs\aheadw2\";

* this is for getting the job codes for wave2 straight;
* this also recodes many variables;
drop _all;
program drop _all;
log using "`saved'merge4.log", replace ;
*set maxvar 20000;

cd `saved';

use `saved'randdata1;


gen lmaxwave=100+RANDwaves;
gen llmaxwave=200+RANDwaves;

*gen lmaxwave=100+9;
*gen llmaxwave=200+9;

*drop if HHID>10000;
*pause;

*gen exasset7=.;
*gen nohouse7=.;
*gen medcost7=.;
*gen dead7=.;
*gen insgov7=.;
*gen bus7=.;
*gen stock7=.;
*gen bonds7=.;
*gen  wheels7=.;
*gen exass7=.;
*gen debts7=.;
*gen liquid7=.;
*gen IRA7=.;
*gen house7=.;
*gen re7=.;
*gen remort7=.; 
*gen mort7=.;
*gen nohou7=.;
*gen trust7=.;
*gen assets7=.; 
* gen _tc7=.;

gen rhoursz1=rhours1;
gen rhoursz2=rhours2;
gen rhoursz3=rhours3;
gen rhoursz4=rhours4;
gen rhoursz5=rhours5;
gen rhoursz6=rhours6;
gen rhoursz7=rhours7;
gen rhoursz8=rhours8;
gen rhoursz9=rhours9;
gen rhoursz10=rhours10;
gen rhoursz11=rhours11;
gen rhoursz12=rhours12;
gen rhoursz13=rhours13;
gen rhoursz14=rhours14;

gen rhourszz1=rhours1;
gen rhourszz2=rhours2;
gen rhourszz3=rhours3;
gen rhourszz4=rhours4;
gen rhourszz5=rhours5;
gen rhourszz6=rhours6;
gen rhourszz7=rhours7;
gen rhourszz8=rhours8;
gen rhourszz9=rhours9;
gen rhourszz10=rhours10;
gen rhourszz11=rhours11;
gen rhourszz12=rhours12;
gen rhourszz13=rhours13;
gen rhourszz14=rhours14;

gen amspouse1=.;
/*
foreach varname in estate7 estateflag7 housebequeathA7 
 housebequeathB7 deathex7 deathins7 inspay7
iprem7 oop7 oopd7 drugc7  tc7 
 {;
  gen `varname'=.;
};
*/

compress;

local j = 2;

while `j'<=14 {;
	local k = (`j'-1);
	while `k'>0 {;
		replace rmarried`j' = rmarried`k' if (rdead`j'==1 & rmarried`j'==.);
		local k = `k' - 1;
		};
	local j = `j' + 1;
};


* rename some variables to keep them under 103 characters;

program define nameshorten;
        version 3.1;
        local i=1;
	while `i'<=RANDwaves[1] {;

rename ghealth`i' gheal`i';
rename inspriv`i' inspr`i';
rename married`i' marr`i';
rename exasset`i' exass`i'; 
rename nohouse`i' nohou`i';
rename medcost`i' medc`i';
rename realage`i' rlage`i';
rename nursing`i' nurs`i';
rename drtimes`i' dr`i';
rename outsurg`i' out`i';
rename dentist`i' dent`i'; 
rename pentypeDB`i'curr pent`i'curr;
rename pentypeDB`i'last pent`i'last;
rename pentypeDB`i'prev pent`i'prev;
rename r`i'cendiv rcendiv`i';
rename r`i'cenreg rcenreg`i';
local i=`i'+1};
end;

nameshorten;


compress;

* make fake waves 102-106;

program define fakewave;
        version 3.1;
        local i=102;
	while `i'<=lmaxwave[1] {;


gen workpref`i'=.; 
gen dead`i'=.; 
gen healch`i'=.;
gen gheal`i'=.;
gen occ`i'=.; 
gen ind`i'=.; 
gen pay`i'=.;
gen wage`i'=.;
gen earn`i'=.; 
gen same`i'=.;
gen epfr`i'=.; 
gen eprfr`i'=.; 

gen school`i'=.; 
gen black`i'=.; 
gen marr`i'=.; 
gen rmarried`i'=.; 
gen children`i'=.; 
* household level - asset and income data;
gen bus`i'=.; 
gen wheels`i'=.; 
gen exass`i'=.; 
gen debts`i'=.;
gen IRA`i'=.; 
gen house`i'=.; 
gen re`i'=.; 
gen nohou`i'=.; 
gen remort`i'=.; 
gen mort`i'=.; 
gen trust`i'=.; 
gen estate`i'=.;
gen estateflag`i'=.;
gen housebequeathA`i'=.;
gen housebequeathB`i'=.;
gen deathex`i'=.;
gen deathins`i'=.;
gen inspay`i'=.;
gen amspouse`i'=.;
*gen aflag`i'=.; 
gen faminc`i'=.; 
gen laby`i'=.; 
gen selfy`i'=.; 
gen socy`i'=.;
gen SSIinc`i'=.;
gen WELFAREinc`i'=.;
gen VAinc`i'=.;
gen capy`i'=.; 
gen othy`i'=.; 
gen peny`i'=.; 
gen tc`i'=.; 
gen unset`i'=.; 
gen oop`i'=.; 
gen oopd`i'=.; 

gen medc`i'=.; 
gen dmedicaid`i'=.;
gen iprem`i'=.; 
gen medhmo`i'=.;

gen hospc`i'=.; 
gen nursc`i'=.; 
gen docc`i'=.; 
gen drugc`i'=.; 
gen hhsc`i'=.; 
gen othels`i'=.; 
gen dentc`i'=.; 
gen outsgc`i'=.; 

* FSK insurance purchase variables;
gen fnpln_gtten`i' = .;
gen fnpln_fivetoten`i' = .;
gen fnpln_three`i' = .;
gen fnpln_one`i' = .;
gen fnpln_fewmo`i' = .;
gen praliv10_mr`i' = .;
gen praliv75_mr`i'= .;
gen cog_recalim`i' = .;
gen cog_mental_status`i' = .;
gen cog_subseven`i' = .;
gen cog_numeracy`i' = .;
gen cog_ticscore`i' = .;
gen risktolFSK`i' = .;
gen risktol_enhanced`i' = .;
gen medigap`i' = .;
gen hgc_ls8th`i'=.;
gen hgc_somehs`i'=.;
gen hgc_hs`i'=.;
gen hgc_somecol`i'=.;
gen hgc_college`i'=.;
gen hgc_gradschl`i'=.;
gen hgc_nr`i'=.;
gen incwi_5k_10k`i'=.;
gen incwi_10k_15k`i'=.;
gen incwi_15k_20k`i'=.;
gen incwi_20k_25k`i'=.;
gen incwi_25k_30k`i'=.;
gen incwi_30k_35k`i'=.;
gen incwi_35k_40k`i'=.;
gen incwi_40k_45k`i'=.;
gen incwi_45k_50k`i'=.;
gen incwi_50plus`i'=.;
gen srphlth_nr`i'=.;
gen srphlth_ex`i'=.;
gen srphlth_vgood`i'=.;
gen srphlth_good`i'=.;
gen srphlth_fair`i'=.;
gen dsrphlth_nr`i'=.;
gen dsrphlth_better`i'=.;
gen dsrphlth_same`i'=.;
gen dsrphlth_worse`i'=.;
gen eversmoke`i'=.;
gen smoker`i'=.;
gen had_hbp`i'=.;
gen had_diabts`i'=.;
gen had_cancer`i'=.;
gen had_emphys`i'=.;
gen had_myocar`i'=.;
gen had_stroke`i'=.;
gen had_psych`i'=.;
gen had_alzhmr`i'=.;
gen had_arth`i'=.;
gen diff_walkblks`i'=.;
gen diff_stoop`i'=.;
gen diff_reach`i'=.;
gen diff_lift`i'=.;
gen diff_dres`i'=.;
gen diff_walk`i'=.;
gen diff_bath`i'=.;
gen diff_eat`i'=.;
gen diff_char`i'=.;
gen diff_toil`i'=.;
gen diff_meal`i'=.;
gen diff_shop`i'=.;
gen diff_tele`i'=.;
gen diff_bils`i'=.;

gen had_hbp_nr`i'=.;
gen had_diabts_nr`i'=.;
gen had_cancer_nr`i'=.;
gen had_emphys_nr`i'=.;
gen had_myocar_nr`i'=.;
gen had_stroke_nr`i'=.;
gen had_psych_nr`i'=.;
gen had_alzhmr_nr`i'=.;
gen had_arth_nr`i'=.;
gen diff_walkblks_nr`i'=.;
gen diff_stoop_nr`i'=.;
gen diff_reach_nr`i'=.;
gen diff_lift_nr`i'=.;
gen diff_dres_nr`i'=.;
gen diff_walk_nr`i'=.;
gen diff_bath_nr`i'=.;
gen diff_eat_nr`i'=.;
gen diff_char_nr`i'=.;
gen diff_toil_nr`i'=.;
gen diff_meal_nr`i'=.;
gen diff_shop_nr`i'=.;
gen diff_tele_nr`i'=.;
gen diff_bils_nr`i'=.;


gen help_dres`i'=.;
gen help_walk`i'=.;
gen help_bath`i'=.;
gen help_eat`i'=.;
gen help_char`i'=.;
gen help_toil`i'=.;
gen help_meal`i'=.;
gen help_shop`i'=.;
gen help_tele`i'=.;
gen help_bils`i'=.;
gen race_othr`i'=.;      
gen marst_married`i'=.;  
gen marst_widow`i'=.;    
gen marst_divor`i'=.;    
gen marst_separ`i'=.;    
gen working`i'=.;        
gen h_resst`i'=.;


* new OOP  ;
gen rhospc`i' = .;	
gen rnursc`i' = .;	
gen routsgc`i' = .;	
gen rdocc`i' = .;	
gen rdentc`i' = .;
gen rdrugc`i'  = .; 	
gen rhhsc`i'  = .;
gen rothels`i' = .;
gen shospc`i' = .;	
gen snursc`i' = .;	
gen soutsgc`i' = .;	
gen sdocc`i' = .;	
gen sdentc`i' = .;
gen sdrugc`i'  = .; 	
gen shhsc`i'  = .;
gen sothels`i' = .;


* spousal variables;
gen _dead`i'=.; 
gen _oop`i'=.; 
gen _oopd`i'=.; 
gen _drugc`i'=.; 
gen _iprem`i'=.; 
gen _medhmo`i'=.;
gen _medc`i'=.; 
gen _tc`i'=.; 
gen _unset`i'=.; 
gen _earn`i'=.; 
gen _hosp`i'=.; 
gen _nurs`i'=.; 
gen _dr`i'=.; 
gen _out`i'=.; 
gen _dent`i'=.; 
gen _nod`i'=.; 
gen _insg`i'=.; 
gen _medcare`i'=.; 
gen _epins`i'=.; 
gen _eprhi`i'=.; 
gen _inssp`i'=.; 
gen _inspr`i'=.; 
gen _insexch`i'=.; 
gen _age`i'=.;
gen hosp`i'=.; 
gen nurs`i'=.; 
gen dr`i'=.; 
gen out`i'=.; 
gen dent`i'=.; 
gen nodrug`i'=.; 
gen pennage`i'=.; 
gen peneage`i'=.; 
gen pent`i'curr=.; 
gen pent`i'last=.; 
gen pent`i'prev=.; 
gen pentypeDC`i'curr=.; 
gen pentypeDC`i'last=.; 
gen pentypeDC`i'prev=.; 
gen penwDC`i'=.;
gen penwDC`i'curr=.;
gen penwDC`i'last=.;
gen penwDC`i'prev=.;
gen tenure`i'=.; 
gen tenurelast`i'=.; 
gen tenureprev`i'=.; 

gen earnlast`i'=.; 
gen earnprev`i'=.; 
gen wagelast`i'=.; 
gen wageprev`i'=.;

gen socc`i'=.; 
gen reason`i'=.;
gen switch`i'=.;
gen job`i'=.; 
gen roopd`i'=.;
gen _roopd`i' =.;

gen rtc`i'=.;
gen rhosp`i'=.;
gen rnursing`i'=.;
gen rdrtimes`i'=.;
gen rdruguse`i'=.;
gen routsurg`i'=.;
gen rdentist`i'=.;

gen rdisabled`i'=.;


gen rhouse`i' =.;
gen rhouse_flag`i' =.;
gen r`i'mstat=.;
gen rmort`i' =.;
gen rwheels`i' =.;
gen rIRA`i' =.;
gen rbus`i' =.;
gen rre`i' = .;
gen rexasset`i' =.;
gen rdebts`i' =.;
gen rnohouse`i' =.;
gen rocc`i'=.;
gen _rocc`i' =.;
gen rind`i'  =.;
gen _rind`i' =.;
gen _rhours`i' =.;
gen rlfpr`i' =.;
gen rlfprz`i' =.;
gen rlfprzz`i' =.;
gen _rlfpr`i' =.;
gen rweeks`i' =.;
gen _rweeks`i' =.;
gen rhours2job`i'=.;
gen _rhours2job`i'=.;
gen rweeks2job`i' =.;
gen _rweeks2job`i' =.;
gen rwage`i' =.;
gen _rwage`i' =.;
gen rearn`i' =.;
gen _rearn`i' =.;
gen rLABY`i' =.;
gen _rLABY`i' =.;
gen rCAPY`i' =.;
gen rPENY`i' =.;
gen rfaminc`i'=.;
gen _rPENY`i' =.;
gen rSOCY`i' =.;
gen _rSOCY`i' =.;
gen rSSDIY`i' =.;
gen _rSSDIY`i' =.;
gen rUNEMPY`i'=.;
gen rOTHgovY`i' =.;
gen rOTHincY`i' =.;
gen _rOTHgovY`i' =.;
gen _rheal`i' =.;
gen radl`i' =.;
gen _radl`i' =.;
gen rhealch`i' =.;
gen _rhealch`i' =.;
gen rdead`i' =.;
gen _rdead`i' =.;
gen rtenure`i'=.;
gen _rtenure`i'=.;
*gen rmedcare`i'=.;
*gen reprhi`i'=.;
*gen rinssp`i'=.;
gen rinspr`i'=.;
gen rrlage`i'=.;

compress;

local i=`i'+1};
end};
fakewave;

*pause;

****no lfpra1;
local i = 2;
while `i'<=14 {;
	gen hours10`i'=.; replace hours10`i'=2000 if lfpra`i'==1; replace hours10`i'=0 if lfpra`i'==0;
	local i = `i' + 1;
};

local i = 2;
local j = 1;

while `i'<=14 {;
	gen heal10`i'=heal`j';
	local i = `i' + 1;
	local j = `j' + 1;
};

****no rlfpra1??? -- right, it does not exist;
local i = 2;
while `i'<=14 {;
	gen rhours10`i'=.; replace rhours10`i'=2000 if rlfpra`i'==1; replace rhours10`i'=0 if rlfpra`i'==0;
	gen rhoursz10`i'=.; replace rhoursz10`i'=2000 if rlfprza`i'==1; replace rhoursz10`i'=0 if rlfprza`i'==0;
	gen rhourszz10`i'=.; replace rhourszz10`i'=2000 if rlfprzza`i'==1; replace rhourszz10`i'=0 if rlfprzza`i'==0;

	local i = `i' + 1;
};

local i = 2;
local j = 1;

while `i'<=14 {;
	gen rheal10`i'=rheal`j';
	local i = `i' + 1;
	local j = `j' + 1;
};

forval i=10/14 {;
rename rhours10`i' rhours1`i';
rename rhoursz10`i' rhoursz1`i';
rename rhourszz10`i' rhourszz1`i';
rename rheal10`i' rheal1`i';
rename hours10`i' hours1`i';
rename heal10`i' heal1`i';
};

*insurance variables;
local i = 2;
local j = 1;

while `i'<=9 {;
    gen rcendiv10`i'=rcendiv`j';
	gen rcenreg10`i'=rcenreg`j';
	gen wgthh10`i'=wgthh`j';
	gen wgtr10`i'=wgtr`j';
	gen epins10`i'=epins`j';
	gen insgov10`i'=insgov`j';
	gen medcare10`i'=medcare`j';
	gen eprhi10`i'=eprhi`j';
	gen inssp10`i'=inssp`j';
	gen inspr10`i'=inspr`j';
	gen insexch10`i'=insexch`j';
	gen age10`i'=age`i'-1;
	gen rlage10`i'=rlage`i'-1;
	gen reprhi10`i'=reprhi`j';
	gen repins10`i'=repins`j';
	gen rinspriv10`i'=rinspriv`j';
	gen rmedcare10`i'=rmedcare`j';
	gen rmedicaid10`i'=rmedicaid`j';
	gen rinsgov10`i'=rinsgov`j';
	gen rinssp10`i'=rinssp`j';
	gen rage10`i'=rage`i'-1;
	gen liquid10`i'=liquid`j';
	gen stock10`i'=stock`j';
	gen bonds10`i'=bonds`j';
	gen assets10`i'=assets`j';
	gen rliquid10`i'=rliquid`j';
	gen rstock10`i'=rstock`j';
	gen rbonds10`i'=rbonds`j';
	gen rassets10`i'=rassets`j';
	gen work10`i'=.; replace work10`i'=work`j' if hours10`i'>0; replace work10`i'=work`i' if hours10`i'>0 & hours`j'==0 & hours`i'>0;
	gen rwork10`i'=.;
	gen _rage10`i'=_rage`i'-1;
	local i = `i' + 1;
	local j = `j' + 1;
};
local i=10;
while `i'<=14 {;
    gen rcendiv1`i'=rcendiv`j';
    gen rcenreg1`i'=rcenreg`j';
	gen wgthh1`i'=wgthh`j';
	gen wgtr1`i'=wgtr`j';
	gen epins1`i'=epins`j';
	gen insgov1`i'=insgov`j';
	gen medcare1`i'=medcare`j';
	gen eprhi1`i'=eprhi`j';
	gen inssp1`i'=inssp`j';
	gen inspr1`i'=inspr`j';
	gen insexch1`i'=insexch`j';
	gen age1`i'=age`i'-1;
	gen rlage1`i'=rlage`i'-1;
	gen reprhi1`i'=reprhi`j';
	gen repins1`i'=repins`j';
	gen rinspriv1`i'=rinspriv`j';
	gen rmedcare1`i'=rmedcare`j';
	gen rmedicaid1`i'=rmedicaid`j';
	gen rinsgov1`i'=rinsgov`j';
	gen rinssp1`i'=rinssp`j';
	gen rage1`i'=rage`i'-1;
	gen liquid1`i'=liquid`j';
	gen stock1`i'=stock`j';
	gen bonds1`i'=bonds`j';
	gen assets1`i'=assets`j';
	gen rliquid1`i'=rliquid`j';
	gen rstock1`i'=rstock`j';
	gen rbonds1`i'=rbonds`j';
	gen rassets1`i'=rassets`j';
	gen work1`i'=.; replace work1`i'=work`j' if hours1`i'>0; replace work1`i'=work`i' if hours1`i'>0 & hours`j'==0 & hours`i'>0;
	gen rwork1`i'=.;
	gen _rage1`i'=_rage`i'-1;
	local i = `i' + 1;
	local j = `j' + 1;
};

compress;

* make fake waves 202-207;
program define fakewave2;
        version 3.1;
        local i=202;
	while `i'<= llmaxwave[1] {;
	

gen workpref`i'=.; 	
gen dead`i'=.; 
gen healch`i'=.;
gen gheal`i'=.;
gen occ`i'=.; 
gen ind`i'=.; 
gen pay`i'=.; 
gen wage`i'=.; 
gen earn`i'=.; 
gen same`i'=.; 
gen epfr`i'=.; 
gen eprfr`i'=.; 
gen school`i'=.; 
gen black`i'=.; 
gen marr`i'=.; 
gen rmarried`i'=.; 
gen children`i'=.; 
gen bus`i'=.; 
gen wheels`i'=.; 
gen exass`i'=.; 
gen debts`i'=.; 
gen IRA`i'=.; 
gen house`i'=.; 
gen re`i'=.; 
gen remort`i'=.; 
gen mort`i'=.; 
gen nohou`i'=.; 
gen trust`i'=.;
gen estate`i'=.;
gen estateflag`i'=.;
gen housebequeathA`i'=.; 
gen housebequeathB`i'=.; 
gen deathex`i'=.;
gen deathins`i'=.;
gen inspay`i'=.;
gen amspouse`i'=.;
*gen aflag`i'=.; 
gen faminc`i'=.; 
gen laby`i'=.;
gen selfy`i'=.; 
gen socy`i'=.; 
gen SSIinc`i'=.;
gen WELFAREinc`i'=.;
gen VAinc`i'=.;
gen capy`i'=.; 
gen othy`i'=.; 
gen peny`i'=.; 
gen tc`i'=.; 
gen unset`i'=.; 
gen oop`i'=.;
gen oopd`i'=.; 

gen medc`i'=.; 
gen dmedicaid`i'=.;
gen iprem`i'=.; 
gen medhmo`i'=.;

gen hospc`i'=.; 
gen nursc`i'=.; 
gen docc`i'=.; 
gen hhsc`i'=.; 
gen othels`i'=.; 
gen dentc`i'=.; 
gen outsgc`i'=.; 
gen drugc`i'=.; 

* FSK insurance purchase variables;
gen fnpln_gtten`i' = .;
gen fnpln_fivetoten`i' = .;
gen fnpln_three`i' = .;
gen fnpln_one`i' = .;
gen fnpln_fewmo`i' = .;
gen praliv10_mr`i' = .;
gen praliv75_mr`i'= .;
gen cog_recalim`i' = .;
gen cog_mental_status`i' = .;
gen cog_subseven`i' = .;
gen cog_numeracy`i' = .;
gen cog_ticscore`i' = .;
gen risktolFSK`i' = .;
gen risktol_enhanced`i' = .;
gen medigap`i' = .;
gen hgc_ls8th`i'=.;
gen hgc_somehs`i'=.;
gen hgc_hs`i'=.;
gen hgc_somecol`i'=.;
gen hgc_college`i'=.;
gen hgc_gradschl`i'=.;
gen hgc_nr`i'=.;
gen incwi_5k_10k`i'=.;
gen incwi_10k_15k`i'=.;
gen incwi_15k_20k`i'=.;
gen incwi_20k_25k`i'=.;
gen incwi_25k_30k`i'=.;
gen incwi_30k_35k`i'=.;
gen incwi_35k_40k`i'=.;
gen incwi_40k_45k`i'=.;
gen incwi_45k_50k`i'=.;
gen incwi_50plus`i'=.;
gen srphlth_nr`i'=.;
gen srphlth_ex`i'=.;
gen srphlth_vgood`i'=.;
gen srphlth_good`i'=.;
gen srphlth_fair`i'=.;
gen dsrphlth_nr`i'=.;
gen dsrphlth_better`i'=.;
gen dsrphlth_same`i'=.;
gen dsrphlth_worse`i'=.;
gen eversmoke`i'=.;
gen smoker`i'=.;
gen had_hbp`i'=.;
gen had_diabts`i'=.;
gen had_cancer`i'=.;
gen had_emphys`i'=.;
gen had_myocar`i'=.;
gen had_stroke`i'=.;
gen had_psych`i'=.;
gen had_alzhmr`i'=.;
gen had_arth`i'=.;
gen diff_walkblks`i'=.;
gen diff_stoop`i'=.;
gen diff_reach`i'=.;
gen diff_lift`i'=.;
gen diff_dres`i'=.;
gen diff_walk`i'=.;
gen diff_bath`i'=.;
gen diff_eat`i'=.;
gen diff_char`i'=.;
gen diff_toil`i'=.;
gen diff_meal`i'=.;
gen diff_shop`i'=.;
gen diff_tele`i'=.;
gen diff_bils`i'=.;

gen had_hbp_nr`i'=.;
gen had_diabts_nr`i'=.;
gen had_cancer_nr`i'=.;
gen had_emphys_nr`i'=.;
gen had_myocar_nr`i'=.;
gen had_stroke_nr`i'=.;
gen had_psych_nr`i'=.;
gen had_alzhmr_nr`i'=.;
gen had_arth_nr`i'=.;
gen diff_walkblks_nr`i'=.;
gen diff_stoop_nr`i'=.;
gen diff_reach_nr`i'=.;
gen diff_lift_nr`i'=.;
gen diff_dres_nr`i'=.;
gen diff_walk_nr`i'=.;
gen diff_bath_nr`i'=.;
gen diff_eat_nr`i'=.;
gen diff_char_nr`i'=.;
gen diff_toil_nr`i'=.;
gen diff_meal_nr`i'=.;
gen diff_shop_nr`i'=.;
gen diff_tele_nr`i'=.;
gen diff_bils_nr`i'=.;

gen help_dres`i'=.;
gen help_walk`i'=.;
gen help_bath`i'=.;
gen help_eat`i'=.;
gen help_char`i'=.;
gen help_toil`i'=.;
gen help_meal`i'=.;
gen help_shop`i'=.;
gen help_tele`i'=.;
gen help_bils`i'=.;
gen race_othr`i'=.;      
gen marst_married`i'=.;  
gen marst_widow`i'=.;    
gen marst_divor`i'=.;    
gen marst_separ`i'=.;    
gen working`i'=.;        
gen h_resst`i'=.;
* new OOP  ;
gen rhospc`i' = .;	
gen rnursc`i' = .;	
gen routsgc`i' = .;	
gen rdocc`i' = .;	
gen rdentc`i' = .;
gen rdrugc`i'  = .; 	
gen rhhsc`i'  = .;
gen rothels`i' = .;
gen shospc`i' = .;	
gen snursc`i' = .;	
gen soutsgc`i' = .;	
gen sdocc`i' = .;	
gen sdentc`i' = .;
gen sdrugc`i'  = .; 	
gen shhsc`i'  = .;
gen sothels`i' = .;

* spousal variables;
gen _dead`i'=.; 
gen _oop`i'=.; 
gen _oopd`i'=.; 
gen _drugc`i'=.; 
gen _iprem`i'=.;
gen _medhmo`i'=.; 
gen _medc`i'=.; 
gen _tc`i'=.; 
gen _unset`i'=.; 
gen _earn`i'=.; 
gen _hosp`i'=.; 
gen _nurs`i'=.;
gen _dr`i'=.; 
gen _out`i'=.; 
gen _dent`i'=.; 
gen _nod`i'=.; 
gen _insg`i'=.; 
gen _medcare`i'=.;  
gen _epins`i'=.; 
gen _eprhi`i'=.; 
gen _inssp`i'=.; 
gen _inspr`i'=.; 
gen _insexch`i'=.; 
gen _age`i'=.;
gen hosp`i'=.; 
gen nurs`i'=.; 
gen dr`i'=.; 
gen out`i'=.;
gen dent`i'=.;
gen nodrug`i'=.; 
gen pennage`i'=.; 
gen peneage`i'=.; 
gen pent`i'curr=.; 
gen pent`i'last=.; 
gen pent`i'prev=.; 
gen pentypeDC`i'curr=.; 
gen pentypeDC`i'last=.; 
gen pentypeDC`i'prev=.; 
gen penwDC`i'=.;
gen penwDC`i'curr=.;
gen penwDC`i'last=.;
gen penwDC`i'prev=.;
gen tenure`i'=.; 
gen tenurelast`i'=.; 
gen tenureprev`i'=.; 

gen earnlast`i'=.; 
gen earnprev`i'=.; 
gen wagelast`i'=.; 
gen wageprev`i'=.;

gen socc`i'=.; 
gen reason`i'=.;
gen switch`i'=.;
gen job`i'=.; 
gen roopd`i'=.;
gen _roopd`i' =.;


gen rtc`i'=.;
gen rhosp`i'=.;
gen rnursing`i'=.;
gen rdrtimes`i'=.;
gen rdruguse`i'=.;
gen routsurg`i'=.;
gen rdentist`i'=.;

gen rdisabled`i'=.;


gen rhouse`i' =.;
gen rhouse_flag`i' =.;
gen r`i'mstat=.;
gen rmort`i' =.;
gen rwheels`i' =.;
gen rIRA`i' =.;
gen rbus`i' =.;
gen rre`i' = .;
gen rexasset`i' =.;
gen rdebts`i' =.;
gen rnohouse`i' =.;
gen rocc`i'=.;
gen _rocc`i' =.;
gen rind`i'  =.;
gen _rind`i' =.;
gen _rhours`i' =.;
gen rlfpr`i' =.;
gen rlfprz`i' =.;
gen rlfprzz`i' =.;
gen _rlfpr`i' =.;
gen rweeks`i' =.;
gen _rweeks`i' =.;
gen rhours2job`i'=.;
gen _rhours2job`i'=.;
gen rweeks2job`i' =.;
gen _rweeks2job`i' =.;
gen rwage`i' =.;
gen _rwage`i' =.;
gen rearn`i' =.;
gen _rearn`i' =.;
gen rLABY`i' =.;
gen _rLABY`i' =.;
gen rCAPY`i' =.;
gen rPENY`i' =.;
gen rfaminc`i'=.;
gen _rPENY`i' =.;
gen rSOCY`i' =.;
gen _rSOCY`i' =.;
gen rOTHgovY`i' =.;
gen rOTHincY`i' =.;
gen rSSDIY`i' =.;
gen _rSSDIY`i' =.;
gen rUNEMPY`i'=.;
gen _rOTHgovY`i' =.;
gen _rheal`i' =.;
gen radl`i' =.;
gen _radl`i' =.;
gen rhealch`i' =.;
gen _rhealch`i' =.;
gen rdead`i' =.;
gen _rdead`i' =.;
gen rtenure`i'=.;
gen _rtenure`i'=.;
*gen rmedcare`i'=.;
*gen reprhi`i'=.;
*gen rinssp`i'=.;
gen rinspr`i'=.;
gen rrlage`i'=.;
sum rrlage*;
compress;
local i=`i'+1};
end;
fakewave2;

*pause;

local i = 2;
local j = 1;

while `i'<=9 {;
	gen heal20`i'=heal`j';
	gen hours20`i'=.; replace hours20`i'=2000 if lfprb`i'==1; replace hours20`i'=0 if lfprb`i'==0;
	gen rheal20`i'=rheal`j';
	gen rhours20`i'=.; replace rhours20`i'=2000 if rlfprb`i'==1; replace rhours20`i'=0 if rlfprb`i'==0;
	gen rhoursz20`i'=.; replace rhoursz20`i'=2000 if rlfprzb`i'==1; replace rhoursz20`i'=0 if rlfprzb`i'==0;
	gen rhourszz20`i'=.; replace rhourszz20`i'=2000 if rlfprzzb`i'==1; replace rhourszz20`i'=0 if rlfprzzb`i'==0;
	gen rcendiv20`i'=rcendiv`j';
	gen rcenreg20`i'=rcenreg`j';
	gen wgthh20`i'=wgthh`j'; 
	gen wgtr20`i'=wgtr`j'; 
	gen epins20`i'=epins`j'; 
	gen insgov20`i'=insgov`j';
	gen medcare20`i'=medcare`j';
	gen eprhi20`i'=eprhi`j';
	gen inssp20`i'=inssp`j';
	gen inspr20`i'=inspr`j';
	gen insexch20`i'=insexch`j';
	gen age20`i'=age`i'-2;
	gen rlage20`i'=rlage`i'-2;
	gen repins20`i'=repins`j';
	gen rinspriv20`i'=rinspriv`j';
	gen rmedcare20`i'=rmedcare`j';
	gen rmedicaid20`i'=rmedicaid`j';
	gen rinsgov20`i'=rinsgov`j';
	gen reprhi20`i'=reprhi`j';
	gen rinssp20`i'=rinssp`j';
	gen rage20`i'=rage`i'-2;
	gen stock20`i'=stock`j';
	gen bonds20`i'=bonds`j';
	gen liquid20`i'=liquid`j';
	gen rstock20`i'=rstock`j';
	gen rbonds20`i'=rbonds`j';
	gen rliquid20`i'=rliquid`j';
	gen assets20`i'=assets`j';
	gen rassets20`i'=rassets`j';
	gen work20`i'=.; replace work20`i'=work`j' if hours20`i'>0;
	replace work20`i'=work`i' if hours20`i'>0 & hours`j'==0 & hours`i'>0;
	gen rwork20`i'=.; 
	gen _rage20`i'=_rage`i'-2;
	summ age2 age3 age4 age5;
	count if ((age`i'<=age`j' & age`j'~=.)|age`i'==.);
	gen dump`i'=0; replace dump`i'=1 if ((age`i'<=age`j' & age`j'~=.)|age`i'==.);
	gen dump10`i'=0; replace dump10`i'=1 if (age10`i'<=age`j' & age`j'~=.)| age10`i'==.;
	gen dump20`i'=0; replace dump20`i'=1 if (age20`i'<=age`j' & age`j'~=.)|age20`i'==.;
	gen rdump`i'=0; replace rdump`i'=1 if ((rage`i'<=rage`j' & rage`j'~=.)|rage`i'==.);
	gen rdump10`i'=0; replace rdump10`i'=1 if (rage10`i'<=rage`j' & rage`j'~=.)|rage10`i'==.;
	gen rdump20`i'=0; replace rdump20`i'=1 if (rage20`i'<=rage`j' & rage`j'~=.)|rage20`i'==.;
	local i = `i' + 1;
	local j = `j' + 1;
};
local i=10;
while `i'<=14 {;
	gen heal2`i'=heal`j';
	gen hours2`i'=.; replace hours2`i'=2000 if lfprb`i'==1; replace hours2`i'=0 if lfprb`i'==0;
	gen rheal2`i'=rheal`j';
	gen rhours2`i'=.; replace rhours2`i'=2000 if rlfprb`i'==1; replace rhours2`i'=0 if rlfprb`i'==0;
	gen rhoursz2`i'=.; replace rhoursz2`i'=2000 if rlfprzb`i'==1; replace rhoursz2`i'=0 if rlfprzb`i'==0;
	gen rhourszz2`i'=.; replace rhourszz2`i'=2000 if rlfprzzb`i'==1; replace rhourszz2`i'=0 if rlfprzzb`i'==0;
	gen rcendiv2`i'=rcendiv`j';
	gen rcenreg2`i'=rcenreg`j';	
	gen wgthh2`i'=wgthh`j';
	gen wgtr2`i'=wgtr`j';
	gen epins2`i'=epins`j'; 
	gen insgov2`i'=insgov`j';
	gen medcare2`i'=medcare`j';
	gen eprhi2`i'=eprhi`j';
	gen inssp2`i'=inssp`j';
	gen inspr2`i'=inspr`j';
	gen insexch2`i'=insexch`j';
	gen age2`i'=age`i'-2;
	gen rlage2`i'=rlage`i'-2;
	gen repins2`i'=repins`j';
	gen rinspriv2`i'=rinspriv`j';
	gen rmedcare2`i'=rmedcare`j';
	gen rmedicaid2`i'=rmedicaid`j';
	gen rinsgov2`i'=rinsgov`j';
	gen reprhi2`i'=reprhi`j';
	gen rinssp2`i'=rinssp`j';
	gen rage2`i'=rage`i'-2;
	gen stock2`i'=stock`j';
	gen bonds2`i'=bonds`j';
	gen liquid2`i'=liquid`j';
	gen rstock2`i'=rstock`j';
	gen rbonds2`i'=rbonds`j';
	gen rliquid2`i'=rliquid`j';
	gen assets2`i'=assets`j';
	gen rassets2`i'=rassets`j';
	gen work2`i'=.; replace work2`i'=work`j' if hours2`i'>0;
	replace work2`i'=work`i' if hours2`i'>0 & hours`j'==0 & hours`i'>0;
	gen rwork2`i'=.; 
	gen _rage2`i'=_rage`i'-2;
	summ age2 age3 age4 age5;
	count if ((age`i'<=age`j' & age`j'~=.)|age`i'==.);
	gen dump`i'=0; replace dump`i'=1 if ((age`i'<=age`j' & age`j'~=.)|age`i'==.);
	gen dump1`i'=0; replace dump1`i'=1 if (age1`i'<=age`j' & age`j'~=.)| age1`i'==.;
	gen dump2`i'=0; replace dump2`i'=1 if (age2`i'<=age`j' & age`j'~=.)|age2`i'==.;
	gen rdump`i'=0; replace rdump`i'=1 if ((rage`i'<=rage`j' & rage`j'~=.)|rage`i'==.);
	gen rdump1`i'=0; replace rdump1`i'=1 if (rage1`i'<=rage`j' & rage`j'~=.)|rage1`i'==.;
	gen rdump2`i'=0; replace rdump2`i'=1 if (rage2`i'<=rage`j' & rage`j'~=.)|rage2`i'==.;
	local i = `i' + 1;
	local j = `j' + 1;
};
	gen dump1=0; replace dump1=1 if age1==.;
	gen rdump1=0; replace rdump1=1 if rage1==.;




compress;


save `saved'temp, replace;


use `saved'temp;



/*
AT SOME POINT, I WILL HAVE A COMPUTER THAT IS SO POWERFUL THAT THIS MIGHT WORK.
NOT YET DEBUGGED.
list HHID PN if HHID[_n] == HHID[_n+1] & PN[_n] == PN[_n+1];
compress;
gen str103 uid = string(HHID) + string(PN);
drop if HHID == .;

keep uid HHID PN sub1w1 sub2w2 sub3w3 sub4w4 sub5w5 sub6w6 dead* healch* gheal* occ* ind* hours* earn* pay* wage*
 heal* epins* wgthh* insgov* medcare* epfr* eprhi* eprfr* inssp* inspr*
 age* rlage* marr* rmarried* children* bus* stock* bonds* 
 wheels* exass* debts* liquid* IRA* house* re* remort* mort* nohou* trust*
 assets* 
estate* estateflag* housebequeathA* housebequeathB* death* inspay*
iprem* oop* oopd* drugc* medc* tc*   hospc* nursc* docc* hhsc* othels* dentc* outsgc*      unset* dump* rdump* work*
 _dead* _oop* _oopd* _drugc* _iprem* _medc* _tc* _earn* _peny* _socy*  _hosp* _nurs* _dr* 
 _out* _dent* _nod* 
 _insg* _medcare* _epins* _eprhi* _inssp* _inspr* _age* hosp* nurs* dr* out* dent* nodrug* 
 tenure* socc* reason* pennage* peneage* 
pent*curr pent*past pentypeDC*curr pentypeDC*last pentypeDC*prev penwDC*curr penwDC*last penwDC*prev switch* job* faminc* laby* selfy* socy* 
 capy* othy* peny* same2 same3 same4 same5 male black school hrscoh birthyr
 roopd* rtc* rhosp* rnursing* rdrtimes* rdruguse* routsurg* rdentist*
_roopd*  rhouse*  rmort* rwheels* rIRA* 
 rliquid*  rstock* rbus* rre* rbonds* rexasset* rdebts* rnohouse* rassets* rocc* _rocc* rind*  
 _rind* rhours* _rhours* rlfpr* _rlfpr*  rweeks*  _rweeks* rhours2job* _rhours2job* 
 rweeks2job* _rweeks2job* rwage* _rwage* rinsgov* rmedcare* medicaid* repins* rearn* _rearn* 
 rLABY* _rLABY* rCAPY* rPENY* rfaminc* _rPENY* rSOCY* _rSOCY* rOTHgovY* rSSDIY* _rSSDIY* rUNEMPY* rOTHincY* _rOTHgovY* 
 rheal* _rheal* rhealch* _rhealch*  rdead* _rdead*  
 rtenure* _rtenure* rage* _rage* rinssp* reprhi* rblack rhispan rbirthm rbirthyr V*;

reshape long _earn _peny _socy _age _faminc_ selfy _laby _capy _peny _socy _othy _medcare _insg _epsin _inspr _eprhi _inssp s_medcare s_insgov s_epins s_insspa s_insspb, i(uid) j(wave);
reshape long dead heal healch ghealth occ ind hours wage earn pay insgov medcare wgthh epins epfr eprhi eprfr inssp inspriv age realage married children rmarried bus stock bonds wheels exasset debts liquid IRA remort house mort re trust nohouse assets estate estateflag housebequeathA housebequeathB deathex deathins inspay aflag faminc laby selfy socy capy othy peny drugc iprem medcost oop oopd tc unset dump rdump work _dead _oop _oopd _drugc _iprem _medcost _tc _earn _peny _socy  _hosp _nursing _drtimes _outsurg _dentist _nodrug _insgov _medcare _epins _eprhi _inssp _inspriv _realage hosp nursing drtimes outsurg dentist nodrug tenure tenurelast tenureprev earnlast earnprev wagelast socc reason pennage peneage pentypeDBcurr pentypeDCcurr penwDCcurr pentypeDBpast pentypeDCpast penwDCpast switch job roopd rtc rhosp rnursing rdrtimes rdruguse routsurg rdentist _roopd rhouse rmort rwheels rIRA rliquid rstock rbus rre rbonds rexasset rdebts rnohouse rassets rocc _rocc rind _rind rhours rhoursz rhourszz _rhours rlfpr rlfprz rlfprzz _rlfpr rweeks _rweeks rhours2job _rhours2job rweeks2job _rweeks2job rwage _rwage rmedcare rmedicaid rinsgov repins rearn _rearn rLABY _rLABY rCAPY rPENY rfaminc _rPENY rSOCY _rSOCY rOTHgovY rOTHincY _rOTHgovY rSSDIY _rSSDIY rUNEMPY rheal _rheal rhealch _rhealch rdead _rdead rtenure _rtenure rage _rage rinssp reprhi, i(uid) j(wave);

save c:\research\hrs\merge\merge4, replace; 
*/


program define savedata;
        version 3.1;
        local i=1;
	while `i'<=llmaxwave[1] {;
keep HHID PN sub*w* dead`i' healch`i' gheal`i' occ`i' ind`i' hours`i' earn`i' pay`i' wage`i'
 heal`i' wgthh`i' wgtr`i' epins`i' insgov`i' medcare`i' dmedicaid`i' epfr`i' eprhi`i' eprfr`i' inssp`i' inspr`i' insexch`i'
 age`i' rlage`i' marr`i' rmarried`i' children`i' bus`i' stock`i' bonds`i' amspouse`i'
 wheels`i' exass`i' debts`i' liquid`i' IRA`i' house`i' re`i' remort`i' mort`i' nohou`i' trust`i'
 assets`i' 
estate`i' estateflag`i' housebequeathA`i' housebequeathB`i' deathex`i' deathins`i' inspay`i'
iprem`i' medhmo`i' hospc`i' nursc`i' docc`i' hhsc`i' othels`i' dentc`i' outsgc`i' oop`i' oopd`i' drugc`i' medc`i' tc`i' unset`i' dump`i' rdump`i' work`i'
 _dead`i' _oop`i' _oopd`i' _drugc`i' _iprem`i' _medhmo`i' _medc`i' _tc`i' _earn`i' _hosp`i' _nurs`i' _dr`i' 
 _out`i' _dent`i' _nod`i' 
 _insg`i' _medcare`i' _epins`i' _eprhi`i' _inssp`i' _inspr`i' _insexch`i' _age`i' hosp`i' nurs`i' dr`i' out`i' dent`i' nodrug`i' 
 tenure`i'  tenurelast`i' tenureprev`i' earnlast`i' earnprev`i' wagelast`i' wageprev`i' socc`i' reason`i' pennage`i' peneage`i' 
pent`i'curr pent`i'last pent`i'prev  pentypeDC`i'curr pentypeDC`i'last pentypeDC`i'prev penwDC`i' penwDC`i'curr penwDC`i'last penwDC`i'prev switch`i' job`i' faminc`i' laby`i' selfy`i' socy`i' 
 capy`i' othy`i' peny`i' same* male black school hrscoh birthyr deathm deathyr
 roopd`i' rtc`i' rhosp`i' rnursing`i' rdrtimes`i' rdruguse`i' routsurg`i' rdentist`i' rdisabled`i'
_roopd`i'  rhouse`i'  rhouse_flag`i' rmort`i' rwheels`i' rIRA`i' 
 rliquid`i'  rstock`i' rbus`i' rre`i' rbonds`i' rexasset`i' rdebts`i' rnohouse`i' rassets`i' rocc`i' _rocc`i' rind`i'  
 _rind`i' rhours`i' rhoursz`i' rhourszz`i'  _rhours`i' rlfpr`i' rlfprz`i' rlfprzz`i' _rlfpr`i'  rweeks`i'  _rweeks`i' rhours2job`i' _rhours2job`i' 
 rweeks2job`i' _rweeks2job`i' rwage`i' _rwage`i' rmedcare`i' rmedicaid`i' rinsgov`i' repins`i' rinspriv`i' rearn`i' _rearn`i' 
 rLABY`i' _rLABY`i' rCAPY`i' rPENY`i' rfaminc`i' _rPENY`i' rSOCY`i' _rSOCY`i' rOTHgovY`i' rSSDIY`i' _rSSDIY`i' rUNEMPY`i' rOTHincY`i' _rOTHgovY`i' workpref`i'
 rheal`i' _rheal`i' radl`i' _radl`i' rhealch`i' _rhealch`i'  rdead`i' _rdead`i' 
 rtenure`i' _rtenure`i' rage`i' _rage`i' rinssp`i' reprhi`i' SSIinc`i' WELFAREinc`i' VAinc`i' rcendiv`i' rcenreg`i' rblack rhispan rbirthm rbirthyr SScov SSYears /*SSEnd*/ Gov* V* RANDwaves l*maxwave
rhospc`i' 	rnursc`i' 	routsgc`i' 	rdocc`i' 	rdentc`i' 	rdrugc`i'  	rhhsc`i'  rothels`i' 
shospc`i' 	snursc`i' 	soutsgc`i' 	sdocc`i' 	sdentc`i' 	sdrugc`i'  	shhsc`i'  sothels`i'
fnpln_gtten`i' fnpln_fivetoten`i' fnpln_three`i' fnpln_one`i' fnpln_fewmo`i' praliv10_mr`i' praliv75_mr`i' cog_recalim`i'   cog_mental_status`i' cog_subseven`i' cog_numeracy`i' cog_ticscore`i' 
risktolFSK`i' risktol_enhanced`i' medigap`i' hgc_ls8th`i' hgc_somehs`i' hgc_hs`i' hgc_somecol`i' hgc_college`i' hgc_gradschl`i' hgc_nr`i' 
incwi_5k_10k`i'  incwi_10k_15k`i'  incwi_15k_20k`i'  incwi_20k_25k`i'  incwi_25k_30k`i'  incwi_30k_35k`i'  incwi_35k_40k`i'  incwi_40k_45k`i'  incwi_45k_50k`i'  incwi_50plus`i'
srphlth_nr`i'  srphlth_ex`i'  srphlth_vgood`i'  srphlth_good`i'  srphlth_fair`i'
dsrphlth_nr`i'  dsrphlth_better`i'  dsrphlth_same`i'  dsrphlth_worse`i'  
eversmoke`i'  smoker`i'
had_hbp`i'  had_diabts`i'  had_cancer`i'  had_emphys`i'  had_myocar`i'  had_stroke`i' had_psych`i' had_alzhmr`i' had_arth`i'
diff_walkblks`i' diff_stoop`i' diff_reach`i' diff_lift`i' diff_dres`i' diff_walk`i'  diff_bath`i' diff_eat`i' diff_char`i' diff_toil`i' diff_meal`i' diff_shop`i' diff_tele`i' diff_bils`i'
had_hbp_nr`i'  had_diabts_nr`i'  had_cancer_nr`i'  had_emphys_nr`i'  had_myocar_nr`i'  had_stroke_nr`i' had_psych_nr`i' had_alzhmr_nr`i' had_arth_nr`i'
diff_walkblks_nr`i' diff_stoop_nr`i' diff_reach_nr`i' diff_lift_nr`i' diff_dres_nr`i' diff_walk_nr`i' diff_bath_nr`i' diff_eat_nr`i' diff_char_nr`i' diff_toil_nr`i' diff_meal_nr`i' diff_shop_nr`i' diff_tele_nr`i' diff_bils_nr`i'
help_dres`i' help_walk`i' help_bath`i' help_eat`i' help_char`i' help_toil`i' help_meal`i' help_shop`i' help_tele`i' help_bils`i'
race_othr       
marst_married`i'   marst_widow`i'     marst_divor`i'     marst_separ`i'    
working`i'        
h_resst`i' ;
* lines added for OOP variables starting rhospc;
* then next added for FSK variables; 
 
gen wave=`i';

rename workpref`i' workpref;
rename dead`i' dead;
rename heal`i' heal;
rename healch`i' healch;
rename gheal`i' ghealth;
rename occ`i' occ;
rename ind`i' ind;
rename hours`i' hours;
rename wage`i' wage;
rename earn`i' earn;
rename pay`i' pay;
rename insgov`i' insgov;
rename medcare`i' medcare;
rename wgthh`i' wgthh;
rename wgtr`i' wgtr;
rename epins`i' epins;
rename epfr`i' epfr;
rename eprhi`i' eprhi;
rename eprfr`i' eprfr;
rename inssp`i' inssp;
rename inspr`i' inspriv;
rename insexch`i' insexch;
rename age`i' age;
rename rlage`i' realage;
rename marr`i' married;
rename children`i' children; 
rename rmarried`i' rmarried;
rename bus`i' bus;
rename stock`i' stock;
rename bonds`i' bonds;
rename wheels`i' wheels;
rename exass`i' exasset;
rename debts`i' debts;
rename liquid`i' liquid;
rename IRA`i' IRA;
rename remort`i' remort;
rename house`i' house;
rename mort`i' mort;
rename re`i' re;
rename trust`i' trust;
rename nohou`i' nohouse;
rename assets`i' assets;
rename estate`i' estate;
rename estateflag`i' estateflag;
rename housebequeathA`i' housebequeathA;
rename housebequeathB`i' housebequeathB;
rename deathex`i' deathex;
rename deathins`i' deathins;
rename inspay`i' inspay;
*rename aflag`i' aflag;
rename faminc`i' faminc;
rename laby`i' laby;
rename selfy`i' selfy;
rename socy`i' socy;
rename capy`i' capy;
rename othy`i' othy;
rename peny`i' peny;

rename iprem`i' iprem;
rename medhmo`i' medhmo;
rename medc`i' medcost;
rename dmedicaid`i' dmedicaid;
rename oop`i' oop;
rename oopd`i' oopd;
rename tc`i' tc;


* FSK insurance purchase variables;
rename fnpln_gtten`i' fnpln_gtten;
rename fnpln_fivetoten`i' fnpln_fivetoten;
rename fnpln_three`i' fnpln_three;
rename fnpln_one`i' fnpln_one;
rename fnpln_fewmo`i' fnpln_fewmo;
rename praliv10_mr`i' praliv10_mr;
rename praliv75_mr`i' praliv75_mr;
rename cog_recalim`i' cog_recalim;
rename cog_mental_status`i' cog_mental_status;
rename cog_subseven`i' cog_subseven;
rename cog_numeracy`i' cog_numeracy;
rename cog_ticscore`i' cog_ticscore;
rename risktolFSK`i' risktolFSK;
rename risktol_enhanced`i' risktol_enhanced;
rename medigap`i' medigap;
rename hgc_ls8th`i' hgc_ls8th;
rename hgc_somehs`i' hgc_somehs;
rename hgc_hs`i' hgc_hs;
rename hgc_somecol`i' hgc_somecol;
rename hgc_college`i' hgc_college;
rename hgc_gradschl`i' hgc_gradschl;
rename hgc_nr`i' hgc_nr;
rename incwi_5k_10k`i' incwi_5k_10k;
rename incwi_10k_15k`i' incwi_10k_15k;
rename incwi_15k_20k`i' incwi_15k_20k;
rename incwi_20k_25k`i' incwi_20k_25k;
rename incwi_25k_30k`i' incwi_25k_30k;
rename incwi_30k_35k`i' incwi_30k_35k;
rename incwi_35k_40k`i' incwi_35k_40k;
rename incwi_40k_45k`i' incwi_40k_45k;
rename incwi_45k_50k`i' incwi_45k_50k;
rename incwi_50plus`i' incwi_50plus;
rename srphlth_nr`i' srphlth_nr;
rename srphlth_ex`i' srphlth_ex;
rename srphlth_vgood`i' srphlth_vgood;
rename srphlth_good`i' srphlth_good;
rename srphlth_fair`i' srphlth_fair;
rename dsrphlth_nr`i' dsrphlth_nr;
rename dsrphlth_better`i' dsrphlth_better;
rename dsrphlth_same`i' dsrphlth_same;
rename dsrphlth_worse`i' dsrphlth_worse;
rename eversmoke`i' eversmoke;
rename smoker`i' smoker;
rename had_hbp`i' had_hbp;
rename had_diabts`i' had_diabts;
rename had_cancer`i' had_cancer;
rename had_emphys`i' had_emphys;
rename had_myocar`i' had_myocar;
rename had_stroke`i' had_stroke;
rename had_psych`i' had_psych;
rename had_alzhmr`i' had_alzhmr;
rename had_arth`i' had_arth;
rename diff_walkblks`i' diff_walkblks;
rename diff_stoop`i' diff_stoop;
rename diff_reach`i' diff_reach;
rename diff_lift`i' diff_lift;
rename diff_dres`i' diff_dres;
rename diff_walk`i' diff_walk;
rename diff_bath`i' diff_bath;
rename diff_eat`i' diff_eat;
rename diff_char`i' diff_char;
rename diff_toil`i' diff_toil;
rename diff_meal`i' diff_meal;
rename diff_shop`i' diff_shop;
rename diff_tele`i' diff_tele;
rename diff_bils`i' diff_bils;

rename had_hbp_nr`i' had_hbp_nr;
rename had_diabts_nr`i' had_diabts_nr;
rename had_cancer_nr`i' had_cancer_nr;
rename had_emphys_nr`i' had_emphys_nr;
rename had_myocar_nr`i' had_myocar_nr;
rename had_stroke_nr`i' had_stroke_nr;
rename had_psych_nr`i' had_psych_nr;
rename had_alzhmr_nr`i' had_alzhmr_nr;
rename had_arth_nr`i' had_arth_nr;
rename diff_walkblks_nr`i' diff_walkblks_nr;
rename diff_stoop_nr`i' diff_stoop_nr;
rename diff_reach_nr`i' diff_reach_nr;
rename diff_lift_nr`i' diff_lift_nr;
rename diff_dres_nr`i' diff_dres_nr;
rename diff_walk_nr`i' diff_walk_nr;
rename diff_bath_nr`i' diff_bath_nr;
rename diff_eat_nr`i' diff_eat_nr;
rename diff_char_nr`i' diff_char_nr;
rename diff_toil_nr`i' diff_toil_nr;
rename diff_meal_nr`i' diff_meal_nr;
rename diff_shop_nr`i' diff_shop_nr;
rename diff_tele_nr`i' diff_tele_nr;
rename diff_bils_nr`i' diff_bils_nr;

rename help_dres`i' help_dres;
rename help_walk`i' help_walk;
rename help_bath`i' help_bath;
rename help_eat`i' help_eat;
rename help_char`i' help_char;
rename help_toil`i' help_toil;
rename help_meal`i' help_meal;
rename help_shop`i' help_shop;
rename help_tele`i' help_tele;
rename help_bils`i' help_bils;
*rename race_othr`i' race_othr;      
rename marst_married`i' marst_married;  
rename marst_widow`i' marst_widow;    
rename marst_divor`i' marst_divor;    
rename marst_separ`i' marst_separ;    
rename working`i' working;        
rename h_resst`i' h_resst;

* new OOP renames ;

if `i'>=6 & `i'<=15 {;
rename rhospc`i' hospc;	
rename rnursc`i' nursc;	
rename routsgc`i' outsgc;	
rename rdocc`i' docc;	
rename rdentc`i' dentc;
rename rdrugc`i'  drugc; 	
rename rhhsc`i'  hhsc;
rename rothels`i' othels;
rename shospc`i' _hospc;	
rename snursc`i' _nursc;	
rename soutsgc`i' _outsgc;	
rename sdocc`i' _docc;	
rename sdentc`i' _dentc;
rename sdrugc`i'  _drugc; 	
rename shhsc`i'  _hhsc;
rename sothels`i' _othels;
drop  nursc`i'  docc`i' hhsc`i' othels`i' dentc`i' outsgc`i' drugc`i' _drugc`i' 
};
else {;
rename hospc`i'  hospc;
rename nursc`i'  nursc;
rename docc`i'  docc;
rename hhsc`i'  hhsc;
rename othels`i'  othels;
rename dentc`i'  dentc;
rename outsgc`i'  outsgc;
rename drugc`i' drugc;
rename _drugc`i' _drugc;
drop rhospc`i' rnursc`i' routsgc`i' rdocc`i' rdentc`i' rdrugc`i' rhhsc`i' rothels`i' shospc`i' snursc`i' soutsgc`i' sdocc`i' sdentc`i' sdrugc`i' shhsc`i' sothels`i' ;
};

rename unset`i' unset;
rename dump`i' dump;
rename rdump`i' rdump;  
rename work`i' work;
rename _dead`i' _dead;
rename _oop`i' _oop;
rename _oopd`i' _oopd;

rename _iprem`i' _iprem;
rename _medhmo`i' _medhmo;
rename _medc`i' _medcost;
rename _tc`i' _tc;
rename _earn`i' _earn;
rename _hosp`i' _hosp;
rename _nurs`i' _nursing;
rename _dr`i' _drtimes;
rename _out`i' _outsurg;
rename _dent`i' _dentist;
rename _nod`i' _nodrug;
rename _insg`i' _insgov;
rename _medcare`i' _medcare;
rename _epins`i' _epins;
rename _eprhi`i' _eprhi;
rename _inssp`i' _inssp;
rename _inspr`i' _inspriv;
rename _insexch`i' _insexch;
rename _age`i' _realage;
rename hosp`i' hosp;
rename nurs`i' nursing;
rename dr`i' drtimes;
rename out`i' outsurg;
rename dent`i' dentist;
rename nodrug`i' nodrug;
rename tenure`i' tenure;
rename tenurelast`i' tenurelast;
rename tenureprev`i' tenureprev;

rename earnlast`i' earnlast;
rename earnprev`i' earnprev;
rename wagelast`i' wagelast;
rename wageprev`i' wageprev;

rename socc`i' socc;
rename reason`i' reason;
rename pennage`i' pennage;
rename peneage`i' peneage;

rename pent`i'curr penDBcurr;
rename pent`i'last penDBlast;
rename pent`i'prev penDBprev;

rename pentypeDC`i'curr penDCcurr;
rename pentypeDC`i'last penDClast;
rename pentypeDC`i'prev penDCprev;
rename penwDC`i' penwDC;
rename penwDC`i'curr penwDCcurr;
rename penwDC`i'last penwDClast;
rename penwDC`i'prev penwDCprev;

rename switch`i' switch;
rename job`i' job;
rename roopd`i' roopd;

rename rtc`i' rtc;
rename rhosp`i' rhosp;
rename rnursing`i' rnursing;
rename rdrtimes`i' rdrtimes;
rename rdruguse`i' rdruguse;
rename routsurg`i' routsurg;
rename rdentist`i' rdentist;

rename rdisabled`i' disabled;



rename _roopd`i' _roopd;
rename rhouse`i' rhouse;
rename rhouse_flag`i' rhouse_flag;
rename rmort`i' rmort;
rename rwheels`i' rwheels;
rename rIRA`i' rIRA;
rename rliquid`i'  rliquid;
rename rstock`i' rstock;
rename rbus`i' rbus;
rename rre`i' rre;
rename rbonds`i' rbonds;
rename rexasset`i' rexasset;
rename rdebts`i' rdebts;
rename rnohouse`i' rnohouse;
rename rassets`i' rassets;
rename rocc`i' rocc;
rename _rocc`i' _rocc;
rename rind`i'  rind;
rename _rind`i' _rind;
rename rhours`i' rhours;
rename rhoursz`i' rhoursz;
rename rhourszz`i' rhourszz;
rename _rhours`i' _rhours;
rename rlfpr`i' rlfpr;
rename _rlfpr`i' _rlfpr;
rename rlfprz`i' rlfprz;
rename rlfprzz`i' rlfprzz;
rename rweeks`i' rweeks;
rename _rweeks`i' _rweeks;
rename rhours2job`i' rhours2job;
rename _rhours2job`i' _rhours2job;
rename rweeks2job`i' rweeks2job;
rename _rweeks2job`i' _rweeks2job;
rename rwage`i' rwage;
rename _rwage`i' _rwage;
rename rmedcare`i' rmedcare;
rename rmedicaid`i' rmedicaid;
rename rinsgov`i' rinsgov;
rename repins`i' repins;
rename rinspriv`i' rinspriv;
rename rearn`i' rearn;
rename _rearn`i' _rearn;
rename rLABY`i' rLABY;
rename _rLABY`i' _rLABY;
rename rCAPY`i' rCAPY;
rename rPENY`i' rPENY;
rename rfaminc`i' rfaminc;
rename _rPENY`i' _rPENY;
rename rSOCY`i' rSOCY;
rename _rSOCY`i' _rSOCY;
rename rOTHgovY`i' rOTHgovY;
rename rOTHincY`i' rOTHincY;
rename _rOTHgovY`i' _rOTHgovY;
rename rSSDIY`i' rSSDIY;
rename _rSSDIY`i' _rSSDIY;
rename rUNEMPY`i' rUNEMPY;
rename rheal`i' rheal;
rename _rheal`i' _rheal;
rename radl`i' radl;
rename _radl`i' _radl;
rename rhealch`i' rhealch;
rename _rhealch`i'  _rhealch;
rename rdead`i' rdead;
rename _rdead`i'  _rdead;
rename rtenure`i' rtenure;
rename _rtenure`i' _rtenure;
rename rage`i' rage;
rename _rage`i' _rage;
rename rinssp`i' rinssp;
rename reprhi`i' reprhi;
rename SSIinc`i' SSIinc;
rename WELFAREinc`i' WELFAREinc;
rename VAinc`i' VAinc;
rename rcendiv`i' rcendiv;
rename rcenreg`i' rcenreg;

rename amspouse`i' amspouse;

save `saved'hrs`i', replace; 

drop _all;
clear;

use `saved'temp, clear; 

local i=`i'+1;
local i2 = RANDwaves[1]+1;
local i3 = lmaxwave[1]+1;
if `i' == `i2' {;
	local i = 102 ;
};

if `i' == `i3' {;
	local i = 202 ;
};
};
end;

savedata;

drop _all;
program drop _all;
log close ;
